import "dart:math" as math;

import 'package:flutter/material.dart';

import "dart:math" as math;

import 'package:flutter/material.dart';
import 'package:gameplay_flutter/lib_runtime/extension/string_extension/string_text.dart';

class JRLoadingButton extends StatefulWidget {
  /// false：默认正常状态 ture loading 状态
  bool loading;

  /// 按钮的标题
  String title;

  /// 按钮的style
  TextStyle? titleStyle;

  /// 按钮事件
  Function? callback;
  // 是否高亮
  bool? highlight;
  // 按钮高度
  double? height;
  double? width;
  double? iconTitleSpeace;

  JRLoadingButton(
    this.loading,
    this.title, {
    super.key,
    this.titleStyle,
    this.callback,
    this.highlight = false,
    this.height = 40.0,
    this.width = 100,
    this.iconTitleSpeace = 4,
  });
  @override
  _JRLoadingButtonState createState() => _JRLoadingButtonState();
}

class _JRLoadingButtonState extends State<JRLoadingButton>
    with SingleTickerProviderStateMixin {
  AnimationController? _controller;

  @override
  void initState() {
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 100 * 15),
    )..repeat();
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  Widget _btn() {
    return Container(
      decoration: (widget.highlight ?? false)
          ? BoxDecoration(
              color: Color(0xFF8A72FE),
              borderRadius: BorderRadius.circular(8.0),
            )
          : BoxDecoration(
              color: Color(0xFFE0DEEC),
              borderRadius: BorderRadius.circular(8.0),
            ),
      height: widget.height,
      width: widget.width,
      child: TextButton(
        style: ButtonStyle(
          foregroundColor: MaterialStateProperty.all(Color(0xFF8A72FE)),
        ),
        child: widget.title.text(
            color: const Color(0xFFFFFFFF),
            fontSize: 14,
            weight: TextFontWeight.medium),
        onPressed: () {
          widget.callback?.call();
        },
      ),
    );
  }

  Widget _loadingBtn() {
    Widget icon = Image.asset(
      "assets/images/common/common_loading.png",
      width: 18,
      height: 18,
    );

    if (_controller != null) {
      icon = AnimatedBuilder(
        animation: _controller!,
        builder: (_, Widget? child) {
          return Transform.rotate(
            angle: _controller!.value * 2 * math.pi,
            child: child,
          );
        },
        child: icon,
      );
    }

    return Container(
      decoration: BoxDecoration(
        color: Color(0xFF8A72FE),
        borderRadius: BorderRadius.circular(8.0),
      ),
      // height: widget.height,
      height: widget.height,
      width: widget.width,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            margin: const EdgeInsets.only(left: 6.0),
            child: icon,
          ),
// iconTitleSpeace
          SizedBox(
            width: widget.iconTitleSpeace ?? 0,
          ),

          widget.title.text(
              color: Colors.white, fontSize: 14, weight: TextFontWeight.medium)
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return widget.loading ? _loadingBtn() : _btn();
  }
}










/*
import "dart:math" as math;

import 'package:flutter/material.dart';

class JRLoadingButton extends StatefulWidget {



}

class _JRLoadingButtonState extends State<JRLoadingButton>
    with SingleTickerProviderStateMixin {
  AnimationController? _controller;

  @override
  void initState() {
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 100 * 15),
    )..repeat();
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  Widget _btn() {
    return Container(
      decoration: BoxDecoration(
        color: (widget.isActive ?? false)
            ? const Color(0xFF8A72FE)
            : const Color(0xFFA8A4BC),
        borderRadius: BorderRadius.circular(8.0),
      ),
      height: widget.height,
      width: widget.width,
      child: TextButton(
        style: ButtonStyle(
          foregroundColor: WidgetStateProperty.all(const Color(0xFF006CFF)),
        ),
        child: Text(widget.title,
            style: TextStyle(
              fontSize: 16.0,
              color: (widget.isActive ?? false)
                  ? const Color(0xFF8A72FE)
                  : const Color(0xFFA8A4BC),
            )),
        onPressed: () {
          widget.callback?.call();
        },
      ),
    );
  }

  Widget _loadingBtn() {

    if (_controller != null) {
      icon = AnimatedBuilder(
        animation: _controller!,
        builder: (_, Widget? child) {
          return Transform.rotate(
            angle: _controller!.value * 2 * math.pi,
            child: child,
          );
        },
        child: icon,
      );
    }

    print("height ----->:${widget.height}");

    return Container(
      decoration: BoxDecoration(
        color: (widget.highlight ?? false)
            ? const Color(0xFF8A72FE)
            : const Color(0xFFA8A4BC),
        borderRadius: BorderRadius.circular(8.0),
      ),
      height: widget.height,
      width: widget.width,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          if (widget.loading)
            Container(
              margin: const EdgeInsets.only(left: 6.0),
              child: icon,
            ),
          Text(
            // "加载中。。。",
            widget.title,
            style: const TextStyle(color: Color(0xFFFFFFFF), fontSize: 14.0),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // return widget.loading ? _loadingBtn() : _btn();
    return _loadingBtn();
  }
}


 */